package com.ccrfid.rmc6.resource.alarm;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.alarm.AlarmLocationTypeEntity;
import com.ccrfid.rmc6.entity.alarm.AlarmRuleEntity;
import com.ccrfid.rmc6.entity.alarm.AlarmRuleTemplateEntity;
import com.ccrfid.rmc6.pojo.Constants;
import com.ccrfid.rmc6.pojo.dto.LocationAlarmRuleDto;
import com.ccrfid.rmc6.pojo.dto.alarm.AlarmRuleDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.pojo.param.SortParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.alarm.AlarmRuleService;
import com.ccrfid.rmc6.service.sys.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Secured
@Path("alarm-rule")
@Produces(MediaType.APPLICATION_JSON)
public class AlarmRuleResource {
    private static final Logger logger = LoggerFactory.getLogger(AlarmRuleResource.class);
    @Autowired
    private AlarmRuleService alarmRuleService;
    @Autowired
    private DictService dictService;

    @GET
    @Path("locations")
    public Page<LocationAlarmRuleDto> getLocations(@QueryParam("locationType") Integer locationType,
                                                   @QueryParam("locationName") String locationName,
                                                   @QueryParam("alarmType") List<Integer> alarmTypeList,
                                                   @QueryParam("alarmStatus") Integer alarmStatus,
                                                   @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        criteria.add(new Criterion("mapLevel", Criterion.Operator.EQ, Constants.MAP_LEVEL.THREE.id));
        if (locationName != null) {
            criteria.add(new Criterion("locationName", Criterion.Operator.LIKE, '%' + locationName + '%'));
        }
        if (locationType != null) {
            List<Integer> typeList = new ArrayList<>();
            typeList.addAll(dictService.getDictAndSubDictIdListById(locationType));
            criteria.add(new Criterion("locationType", Criterion.Operator.IN, typeList));
        }
        Set<Long> locationIdList = alarmRuleService.getLocationIdList(alarmTypeList, alarmStatus);
        if (locationIdList != null) {
            if (locationIdList.size() > 0) {
                if (alarmStatus != null && alarmStatus.equals(0)) {
                    criteria.add(new Criterion("id", Criterion.Operator.NOT_IN, locationIdList));
                } else {
                    criteria.add(new Criterion("id", Criterion.Operator.IN, locationIdList));
                }
            } else {
                criteria.add(new Criterion("id", Criterion.Operator.EQ, 0));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }

        Page<LocationAlarmRuleDto> page = alarmRuleService.getAlarmLocations(criteria, pageRequest);
        return page;
    }

    //获取某个区域及其相关的布控信息
    @GET
    @Path("location/{locationId}")
    public LocationAlarmRuleDto getLocationAlarmRules(@PathParam("locationId") Long locationId) {
        return alarmRuleService.getLocationAlarmRules(locationId);
    }

    //获取某个区域类型允许设置的布控类型
    @GET
    @Path("location-type/{locationType}/enable-alarm-types")
    public List<AlarmLocationTypeEntity> getLocationAlarmTypes(@PathParam("locationType") Integer locationType) {
        return alarmRuleService.getLocationAlarmTypes(locationType);
    }

    @GET
    @Path("{alarmRuleId}")
    public AlarmRuleEntity getAlarmRule(@PathParam("alarmRuleId") Long alarmRuleId) {
        return alarmRuleService.getAlarmRule(alarmRuleId);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public AlarmRuleEntity createAlarmRule(AlarmRuleEntity entity) throws AppException {
        return alarmRuleService.createAlarmRule(entity);
    }

    @POST
    @Path("batch-to-location-type/{locationType}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Integer batchCreateAlarmRules2LocationType(@PathParam("locationType") Integer locationType,
                                                      AlarmRuleEntity entity) {
        return alarmRuleService.batchCreateAlarmRules2LocationType(locationType, entity).size();
    }

    @PUT
    @Path("{alarmRuleId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public AlarmRuleEntity updateAlarmRule(@PathParam("alarmRuleId") Long alarmRuleId,
                                           AlarmRuleEntity entity) throws AppException {
        return alarmRuleService.updateAlarmRule(alarmRuleId, entity);
    }

    @DELETE
    @Path("{alarmRuleId}")
    public Boolean deleteAlarmRule(@PathParam("alarmRuleId") Long alarmRuleId) throws AppException {
        return alarmRuleService.deleteAlarmRule(alarmRuleId);
    }

    @GET
    @Path("templates")
    public List<AlarmRuleTemplateEntity> getAlarmRuleTemplates(@QueryParam("alarmType") List<Integer> alarmTypeList,
                                                               @BeanParam SortParam sortParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (alarmTypeList != null && alarmTypeList.size() > 0) {
            criteria.add(new Criterion("alarmType", Criterion.Operator.IN, alarmTypeList));
        }
        List<AlarmRuleTemplateEntity> list;
        if (sortParam.getOrders() != null && sortParam.getOrders().size() > 0) {
            list = alarmRuleService.getAlarmRuleTemplates(criteria, Sort.by(sortParam.getOrders()));
        } else {
            list = alarmRuleService.getAlarmRuleTemplates(criteria, Sort.by(new ArrayList<>()));
        }
        return list;
    }

    @GET
    @Path("template/{templateId}")
    public AlarmRuleTemplateEntity getAlarmRuleTemplate(@PathParam("templateId") Long templateId) {
        return alarmRuleService.getAlarmRuleTemplate(templateId);
    }

    @POST
    @Path("template")
    @Consumes(MediaType.APPLICATION_JSON)
    public AlarmRuleTemplateEntity createAlarmRuleTemplate(AlarmRuleTemplateEntity entity) {
        return alarmRuleService.createAlarmRuleTemplate(entity);
    }

    @PUT
    @Path("template/{templateId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public AlarmRuleTemplateEntity updateAlarmRuleTemplate(@PathParam("templateId") Long templateId,
                                                           AlarmRuleTemplateEntity entity) throws AppException {
        return alarmRuleService.updateAlarmRuleTemplate(templateId, entity);
    }

    @DELETE
    @Path("template/{templateId}")
    public Boolean deleteAlarmRuleTemplate(@PathParam("templateId") Long templateId) throws AppException {
        return alarmRuleService.deleteAlarmRuleTemplate(templateId);
    }

    //获取互监布防规则
    @GET
    @Path("mutual-rules")
    public List<AlarmRuleEntity> getMutualAlarmRuleList() {
        return alarmRuleService.getMutualAlarmRuleList();
    }

    //获取所有告警规则名称
    @GET
    @Path("alarm-rule-name")
    public List<String> getAllAlarmRuleName(){
        return alarmRuleService.getAllAlarmRuleName();
    }

    @GET
    @Path("alarm-rule-app")
    public List<AlarmRuleDto> getAlarmRuleForApp(){
        return alarmRuleService.getAlarmRuleForApp();
    }

    @DELETE
    @Path("batch")
    public Boolean deleteAlarmRule(List<Long> alarmRuleIdList) throws AppException {
        return alarmRuleService.batchDeleteAlarmRule(alarmRuleIdList);
    }

}
